package com.yijumao.shop.controller;

import com.github.pagehelper.PageInfo;
import com.yijumao.shop.domain.Item;
import com.yijumao.shop.domain.ItemCat;
import com.yijumao.shop.service.ItemCatService;
import com.yijumao.shop.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/item")
public class ItemController {
    @Autowired
    private ItemService itemService;
    @Autowired
    private ItemCatService itemCatService;
    @Value("${pageInfo.pageSize}")
    private int pageSize;

    //显示商品详细信息
    @RequestMapping("/{id}")
    public String item(@PathVariable int id,Model model){
        Item item = itemService.selectByPrimaryKey(id);
        model.addAttribute("item",item);
        return "item";
    }

    //分页
    @RequestMapping("/cat/{cid}/{pageNum}")
    public String selectByCid(@PathVariable int cid, @PathVariable int pageNum, Model model){
        //获得分页对象
        PageInfo<Item> pageInfo = itemService.selectPageByCid(cid, pageNum, pageSize);
        //获得分类信息对象
        ItemCat itemCat = itemCatService.selectByPrimaryKey(cid);
        //将分页列表和分类信息存入model
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("itemCat", itemCat);
        return "category";
    }

}
